<?php

namespace App\Http\Controllers\OpenShop\FeishuMini;

use App\Http\Controllers\BaseController;
use App\Service\OpenShop\FeishuMini\ProgrammingService;
use App\Service\OpenShop\FeishuMini\SandTableService;
use App\Service\OpenShop\SiteLocation\AuthService;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;

class SandTableController extends BaseController
{
    /**
     * 获得沙盘筛选
     * @param Request $request
     * @return JsonResponse
     */
    public function sift(Request $request): JsonResponse
    {
        return $this->success(SandTableService::sift());
    }


    /**
     * 获得一个城市下区县的中心点
     * @param Request $request
     * @return JsonResponse
     */
    public function getCenterDotByCity(Request $request): JsonResponse
    {
        return $this->success(SandTableService::getCenterDotByCity());
    }


    /**
     * 获得市下面所有的点位信息（根据区分组）
     * @param Request $request
     * @return JsonResponse
     * @throws ValidationException
     */
    public function getProgrammingDotByCity(Request $request): JsonResponse
    {
        $this->validate($request, [
            'cityCode' => 'int|required'
        ]);
        $cityCode = $request->get('cityCode', '');
        $haveAdcode = $request->haveAdcode;
        if (!in_array($cityCode, $haveAdcode)) {
            //$cityCode = $haveAdcode[0] ?? 0;
            return $this->fail(403, '您没有该区域的权限', []);
        }
        return $this->success(SandTableService::getProgrammingDotByCity($cityCode));
    }


    /**
     * 获得市下面所有的点位信息 筛选
     * @param Request $request
     * @return JsonResponse
     * @throws ValidationException
     */
    public function getProgrammingDotByCitySearch(Request $request): JsonResponse
    {
        $this->validate($request, [
            'cityCode' => 'int|required',
        ]);
        $cityCode   = $request->get('cityCode', '');
        $adcode     = $request->get('adcode', 0);
        $subType    = $request->get('subType', '');
        $grade      = $request->get('grade', '');
        $status     = $request->get('status', '');
        $pageNum    = $request->get('pageNum', 1);
        $pageSize   = $request->get('pageSize', 5000);
        $haveAdcode = $request->haveAdcode;
        if (!in_array($cityCode, $haveAdcode)) {
            return $this->fail(403, '您没有该区域的权限', []);
        }
        return $this->success(SandTableService::getProgrammingDotByCitySearch($cityCode, $adcode, $subType, $grade, $status, $pageNum, $pageSize));
    }

    /**
     * 获得点位详情
     * @param Request $request
     * @return JsonResponse
     * @throws ValidationException|GuzzleException
     */
    public function getDotDetail(Request $request): JsonResponse
    {
        $this->validate($request, [
            'type' => 'required',
            'id'   => 'required'
        ]);
        $type      = $request->get('type');    //点位类型
        $id        = $request->get('id');      //主键ID
        $rmsUserId = $request->userId;
        return $this->success(SandTableService::getDotDetail($type, $id, $rmsUserId));
    }
    /**
     * 获得点位详情
     * @param Request $request
     * @return JsonResponse
     * @throws ValidationException|GuzzleException
     */
    public function getDotCardInfo(Request $request): JsonResponse
    {
        $this->validate($request, [
            'type' => 'required',
            'id'   => 'required'
        ]);
        $type      = $request->get('type');    //点位类型
        $id        = $request->get('id');      //主键ID
        $rmsUserId = $request->userId;
        return $this->success(SandTableService::getDotCardInfo($type, $id, $rmsUserId));
    }


    /**
     * 点位搜索
     * @param Request $request
     * @return JsonResponse
     * @throws ValidationException
     */
    public function dotSearch(Request $request): JsonResponse
    {
        $this->validate($request, [
            'type'   => 'required',
            'adcode' => 'required',
        ]);

        $type     = $request->get('type');
        $name     = $request->get('name');
        $adcode   = $request->get('adcode');
        $pageNum  = $request->get('pageNum', 1);
        $pageSize = $request->get('pageSize', 10);

        return $this->success(SandTableService::dotSearch($type, $name, $adcode, $pageNum, $pageSize));
    }


    /**
     * 根据城市Code获得区域边界
     * @param Request $request
     * @return JsonResponse
     */
    public function getBoundaryByCity(Request $request): JsonResponse
    {
        $cityCode = $request->get('cityCode', 0);
        return $this->success(SandTableService::getBoundaryByCity($cityCode));
    }


    /**
     * @throws ValidationException
     */
    public function getKfDotPolymerize(Request $request): JsonResponse
    {
        $this->validate($request, [
            'cityCode' => 'int|required'
        ]);
        $cityCode = $request->get('cityCode', '');
        $haveAdcode = $request->haveAdcode;
        if (!in_array($cityCode, $haveAdcode)) {
            //$cityCode = $haveAdcode[0] ?? 0;
            return $this->fail(403, '您没有该区域的权限', []);
        }
        return $this->success(SandTableService::getKfDotPolymerize($cityCode));
    }

}
